package com.ykdl.member.kid.adapters;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ykdl.member.kid.R;
import com.ykdl.member.kid.beans.FollowerActor;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.messagebox.FansActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;

public class FansAdapter extends BaseAdapter implements OnClickListener{
	private Activity mContext;
	private List<UserActorBean> users = new ArrayList<UserActorBean>();

	private OnAttentionListener mListener;
	public interface OnAttentionListener{
		public void onClickAttention(UserActorBean user);
	}
	
	public void setOnAttentionListener(OnAttentionListener listener){
		this.mListener = listener;
	}
	
	public FansAdapter(Activity context) {
		this.mContext = context;
	}
	
	public void addUser(List<UserActorBean> list){
		users = list ;
	}

	@Override
	public int getCount() {
		return users.size();
	}

	@Override
	public Object getItem(int position) {
		return users.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = RelativeLayout.inflate(mContext, R.layout.fansadapter_item, null);
			holder = new ViewHolder();
			holder.user_head = (AsyncImageView) convertView.findViewById(R.id.user_head);
			holder.user_name = (TextView) convertView.findViewById(R.id.user_name);
			holder.user_grade = (TextView) convertView.findViewById(R.id.grade);
			holder.user_age = (TextView) convertView.findViewById(R.id.user_age);
			holder.signname = (TextView) convertView.findViewById(R.id.signname);
			holder.user_address = (TextView) convertView.findViewById(R.id.user_address);
			holder.is_follow_bt = (Button) convertView.findViewById(R.id.is_follow_bt);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		UserActorBean user = (UserActorBean) getItem(position);
		
		if (user.getFollower_actor()!= null) {
			FollowerActor fa = user.getFollower_actor();
			holder.user_name.setText(fa.getDisplay_name());
			if (fa.getAvatar_file_meta() !=null && fa.getAvatar_file_meta().getDownload_urls() != null &&
					fa.getAvatar_file_meta().getDownload_urls().getMiddle() != null &&
							fa.getAvatar_file_meta().getDownload_urls().getMiddle().getUrl() != null) {
				holder.user_head.setUrl(FansActivity.TAG, fa.getAvatar_file_meta().getDownload_urls().getMiddle().getUrl(), ImageOptionsFactory.getSmallPortraitOptions(mContext));
			}
			if (fa.getBaby_profile() != null) {
				long time = (long)fa.getBaby_profile().getBirthday() * 1000;
				holder.user_age.setText(DateUtil.getBabyAge(time, System.currentTimeMillis()));
			}
			if (fa.getProfile() != null) {
				holder.user_address.setText(fa.getProfile().getRegion());
				if (!TextUtils.isEmpty(fa.getProfile().getSignature())) {
					holder.signname.setText(fa.getProfile().getSignature());
				}
				
			}
			holder.user_grade.setText(String.valueOf(fa.getLevel()));
		}
		if (user.isfriend()) {
			holder.is_follow_bt.setSelected(true);
		} else {
			holder.is_follow_bt.setSelected(false);
			holder.is_follow_bt.setOnClickListener(this);
			holder.is_follow_bt.setTag(user);
		}
		
		return convertView;
	}

	class ViewHolder {
		AsyncImageView user_head;
		TextView user_name, user_grade, user_age, user_address, signname;
		Button is_follow_bt;
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.is_follow_bt) {
			UserActorBean user = (UserActorBean) v.getTag();
			if (mListener != null) {
				mListener.onClickAttention(user);
			}
		}
	}

}
